/*
 * Serum
 *
 * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
 */

Serum.Panel.Packages = Ext.extend(Ext.Panel, {

    /**
     * Constructor
     *
     * @param   {Object}    config
     */
    constructor: function(config) {

        // Constructor
        Serum.Panel.Packages.superclass.constructor.call(this, {
            id:             'feed-tree',
            region:         'west',
            title:          __('base~modules_title'),
            split:          true,
            width:          '15%',
            minSize:        '10%',
            maxSize:        '25%',
            collapsible:    true,
            margins:        '0 0 5 5',
            cmargins:       '0 5 5 5',
            rootVisible:    false,
            lines:          false,
            autoScroll:     true,
            border:         true,

            root:           new Ext.tree.TreeNode(__('base~modules_list')),

            collapseFirst:  false,
            layout:         'accordion',
            items:          this.addPanels(Serum.Packages.get().getValues()),

            layoutConfig: {
                animate:true
            },

            listeners: {
                render: function(c) {
                    //c.body.on('contextmenu', this.onContextMenu, this);
                }
            },
            scope: this
        });
    },

    /**
     * Adds panels to the container
     *
     * @param   {Object}    packages
     */
    addPanels: function(packages) {

        var items       = [];
        var pack        = null;
        var slug        = null;
        var j, i        = 0;

        for (slug in packages) {
            pack = packages[slug];

            if (typeof pack === 'function') {
                continue;
            }

            // Package panel
            var panel = new Serum.Panel.Modules({
                rootVisible:    false,
                title:          pack.name,
                iconCls:        'img-package'
            });

            // Panel's root
            var panelRoot = new Ext.tree.TreeNode({
                text:       pack.name,
                draggable:  false,
                id:         '0'                  // this IS the id of the startnode
            });

            // Adds modules to the root
            for (j = 0; j < pack.modules.length; j++) {
                panelRoot.appendChild(pack.modules[j]);
            }

            panel.setRootNode(panelRoot);

            // Adds the panel to the container
            items.push(panel);
        }

        return items;
    }
});

Serum.Panel.Modules = Ext.extend(Ext.tree.TreePanel, {

    /**
     * Constructor
     *
     * @param   {Object}    config
     */
    constructor: function(config) {
        Serum.Panel.Modules.superclass.constructor.call(this, Ext.apply({
            rootVisible:        false,
            collapsible:        false,
            animCollapse:       false,
            useArrows:          true,
            border:             false,
            autoScroll:         true,
            animate:            false,
            enableDD:           true,
            containerScroll:    false
        }, config));

        this.on('click', this.onSelectElement, this);
    },

    /**
     * Click event handler
     *
     * @param   {Object}    element
     */
    onSelectElement: function(element) {
        Serum.Selector.get().set({module: element.attributes.slug, action: 'index'});
    }
});